package sessionBeans;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import objects.HelpmeetingObject;
import entityBeans.Comment;
import entityBeans.Helpmeeting;
import entityBeans.User;

/**
 *
 * @author user
 */
@Stateless
public class HelpmeetingFacade extends AbstractFacade<Helpmeeting> implements HelpmeetingFacadeLocal {
    @PersistenceContext(unitName = "sw2")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public HelpmeetingFacade() {
        super(Helpmeeting.class);
    }
/*
 *
 * this method will send an empty form to user to participate a meeting
 */
 @Override
      public void addMeeting(Helpmeeting helpmeeting){
      
      }
    
/*
 *
 * this method will save the meeting information into database
 */
 @Override
  public void submitMeeting(Helpmeeting helpmeeting){
  
  }
	@Override
	public List<Helpmeeting> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Helpmeeting> findRange(int[] range) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void commentOnMeeting(User user, HelpmeetingObject meeting,
			Comment comment) throws BeanException {
		if(user != null){
			if(meeting!=null){
				if(comment.getText()!=null){
					comment.setUser(user);
					user.getCommentCollection().add(comment);
					Helpmeeting hmeeting=new Helpmeeting(meeting.getmeetingID());
					comment.setHelpmeeting(hmeeting);
					hmeeting.addCommentToCommentCollection(comment);
					
					em.persist(hmeeting);
					em.persist(comment);
					em.persist(user);
										
				}else{
					throw new BeanException("Comment can not be empty!");
				}
				
			}else{
				throw new BeanException("Empty meeeting there is a problem!");
			}
			
		}else{
			throw new BeanException("There is no such user!");
		}
		
	}
    
}